﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Framework.Mvc.AgencyGiftViewModel>" %>

<script type="text/javascript">
    function Checked() {
        var r = document.getElementsByName("cbgifts");
        var gifs = "";
        for (var i = 0; i < r.length; i++) {
            if (r[i].checked) {
                gifs += r[i].value + ",";
            }
        }
        gifs = gifs.substring(0, gifs.length - 1);
        gifs += "<-customGift->" + document.getElementById("customGift").value;
        var hgifts = document.getElementById("AgencyGifts");
        hgifts.setAttribute("value", gifs);
        //alert(hgifts.value);
    }
</script>
<div>
    <%using (Html.BeginForm())
      {
          string agencyGifts = String.Empty;
          string customGift = String.Empty;
          if (!String.IsNullOrEmpty(this.Model.AgencyGifts))
          {
              var temp = this.Model.AgencyGifts.Split(new string[] { "<-customGift->" }, StringSplitOptions.RemoveEmptyEntries);
              if (temp.Length == 2)
              {
                  customGift = temp[1];
              }
              agencyGifts = temp[0];
          }

          var gifs = agencyGifts.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    %>
    <ul>
        <% foreach (var item in this.Model.Gifts)
           {
        %>
        <li style="list-style: none">
            <input type="checkbox" name="cbgifts" value="<%=item.Name %>" <%= gifs.Contains(item.Name)?"checked='checked'":"" %>
                onclick="javascript:Checked()" />
            <%=item.Name %></li>
        <%
               } %>
    </ul>
    <input type="text" id="customGift" value="<%=customGift %>" onkeyup="javascript:Checked()" />
    <%:Html.HiddenFor(m => m.AgencyGifts)%>
    <%:Html.HiddenFor(m=>m.AgencyIdentity) %>
    <input type="submit" value="提交" />
    <%
          } 
    %>
</div>
